unit GLGUIButton;

interface

uses
  Classes,
  VectorGeometry,
  GLGUIQuad,
  GLGUIWidget,
  GLGUIComponent,
  GLGUIBoundedLabel,
  GLGUIEvent,
  GLGUITokens;

type
  TGLGUIButton = class(TGLGUILabeled)
  protected
    procedure OnFontUpdate(AEvent: TGLGUIEvent); virtual;
  public
    constructor Create(AOwner: TComponent); override;
  end;

  TGLGUIImageButton = class(TGLGUIButton)
  protected
    FIcon: TGLGUIQuad;
    function GetIcon: TGLGUIQuad;
    procedure SetIcon(const AIcon: TGLGUIQuad);
    function GetIconSkinName: String;
    procedure SetIconSkinName(ASkinName: String);
    procedure SetSkinName(const ASkinName: String); override;
    procedure OnFontUpdate(AEvent: TGLGUIEvent); override;
    procedure OnUpdate(AEvent: TGLGUIEvent);
    procedure DoPositionIcon();
  public
    constructor Create(AOwner: TComponent); override;
    property Icon: TGLGUIQuad read GetIcon write SetIcon;
    property IconSkinName: String read GetIconSkinName write SetIconSkinName;
  end;

implementation

constructor TGLGUIButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FLabel.AddEventListener(FontUpdateEvent, OnFontUpdate);
  FLabel.Align := galHorizontal;
  FLabel.Padding.Left := 25;
  FLabel.Padding.Right := 25;
  FLabel.AutoBound := False;
  FLabel.Text := 'TGLGUIButton';
  Height := FLabel.Height + 25;
  FLabel.AutoBound := True;
  CursorImage := -21;
end;

procedure TGLGUIButton.OnFontUpdate(AEvent: TGLGUIEvent);
begin
  FLabel.AutoBound := False;
  FLabel.Text := FLabel.Text;
  Height := FLabel.Height + 25;
  FLabel.AutoBound := True;
end;

constructor TGLGUIImageButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  AddEventListener(GUIChangeEvent, OnUpdate);
end;

function TGLGUIImageButton.GetIcon: TGLGUIQuad;
begin
  Result := FIcon;
end;

procedure TGLGUIImageButton.SetIcon(const AIcon: TGLGUIQuad);
begin
  FIcon := AIcon;
  FIcon.EventBehaviour := ebPassEvent;
  Flabel.Padding.Left := FIcon.Width + 10/2 + 25;
  FLabel.AutoBound := False;
  if Assigned(FIcon) then
    Height := MaxFloat(FLabel.Height, FIcon.Width) + 25
  else
    Height := FLabel.Height + 25;
  FLabel.AutoBound := True;
  DoPositionIcon();
end;

function TGLGUIImageButton.GetIconSkinName: String;
begin
  if Assigned(FIcon) then
    Result := FIcon.SkinName
  else
    Result := '';
end;

procedure TGLGUIImageButton.OnFontUpdate(AEvent: TGLGUIEvent);
var
  vTextChanged:Boolean;
begin
  DoPositionIcon();
  vTextChanged := False;
  if(FLabel.Text = '') then
  begin
    vTextChanged := True;
    FLabel.AutoBound := False;
    FLabel.Text := ' ';
  end;
  if Assigned(FIcon) then
    Height := MaxFloat(FLabel.Height, FIcon.Width) + 25
  else
    Height := FLabel.Height + 25;
  if vTextChanged then
  begin
    FLabel.AutoBound := True;
    FLabel.Text := '';
  end;
end;

procedure TGLGUIImageButton.OnUpdate(AEvent: TGLGUIEvent);
begin
  DoPositionIcon();
end;

procedure TGLGUIImageButton.DoPositionIcon;
begin
  if Assigned(FIcon) then
  begin
    if(FLabel.TextLength>0)then
      FIcon.Position.X := -FLabel.TextWidth/2 - 10/2
    else
      FIcon.Position.X := 0;
    FIcon.Visible := Width > FIcon.Width + 10;
  end;
end;

procedure TGLGUIImageButton.SetIconSkinName(ASkinName: String);
begin
  if Assigned(FIcon) then
    FIcon.SkinName := ASkinName;
end;

procedure TGLGUIImageButton.SetSkinName(const ASkinName: String);
begin
  inherited;
  DoPositionIcon();
end;

end.
